// +----------------------------------------------------------------------
// | Manong.Cloud [ 领酷码农云 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.manong.cloud All rights reserved.
// +----------------------------------------------------------------------
// | Author: 稻草人 <qinuoyun@qq.com>
// +----------------------------------------------------------------------
// | Member: 围棋 飘逸者 Loumt Silence24
// +----------------------------------------------------------------------
import { Validator } from './ValidatorModel.js';


class PersonValidator extends Validator {
    constructor(descriptor, models) {
        super()
        this.models = models;
        this.registration();
        //添加校验规则，参考async-validator
        this.descriptor = descriptor;
    }

    /**
     * 批量注册自定义方法
     */
    registration() {
        const Models = this.models
        Validator.register("unique", (rule, value, callback, source, options) => {
            new Promise((resolve, reject) => {
                let where = {};
                let key = rule.field;
                where[key] = value;
                Models.where(where).first().then(res => {
                    if (res) {
                        reject()
                    } else {
                        resolve()
                    }
                }).catch(error => {
                    reject(error)
                })
            }).then(res => {
                callback()
            }).catch(error => {
                callback(new Error())
            })
        })


    }
}

export default PersonValidator
